package com.shelpe.services.sinbad.controller.sinbad;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shelpe.services.sinbad.controller.dto.BlackUserDto;
import com.shelpe.services.sinbad.entity.jpa.PurchaseOrder;
import com.shelpe.services.sinbad.entity.jpa.User;
import com.shelpe.services.sinbad.entity.mongo.BlackList;
import com.shelpe.services.sinbad.repository.jpa.PurchaseOrderRepository;
import com.shelpe.services.sinbad.repository.jpa.UserRepository;
import com.shelpe.services.sinbad.repository.mongo.BlackListRepository;
import com.shelpe.services.sinbad.utils.DateUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/sinbadservice/v2")
@Api("黑名单相关的API")
public class BlackListController {
	@Autowired
	private BlackListRepository blackListRepository;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private PurchaseOrderRepository purchaseOrderRepository;

	@ApiOperation("黑名单的创建")
	@RequestMapping(path = "/users/{user_id}/blacks", method = RequestMethod.POST)
	public BlackUserDto createBlack(@PathVariable("user_id") long userID, @RequestBody BlackUserDto dto) {
		User user = this.userRepository.findOne(userID);
		Assert.notNull(user, "user_not_exist");
		BlackList po = dto.to();
		po.from(user);
		this.blackListRepository.save(po);
		dto.setStatus(true);

		return dto;
	}

	@ApiOperation("黑名单的移除")
	@RequestMapping(path = "/users/{user_id}/blacks", method = RequestMethod.DELETE)
	public BlackUserDto deleteBlack(@PathVariable("user_id") long userID) {
		BlackUserDto dto = new BlackUserDto();
		BlackList po = this.blackListRepository.findByUserID(userID);
		if (po != null) {
			dto.from(po);
			this.blackListRepository.delete(po);
		}
		return dto;
	}

	@ApiOperation("黑名单的查看")
	@RequestMapping(path = "/users/{user_id}/blacks", method = RequestMethod.GET)
	public BlackUserDto findBlacks(@PathVariable("user_id") long userID) {
		BlackUserDto dto = new BlackUserDto();
		BlackList po = this.blackListRepository.findByUserID(userID);
		if (po != null) {
			dto.from(po);
		}
		return dto;
	}

	@ApiOperation("黑名单的获取")
	@RequestMapping(path = "/blacks", method = RequestMethod.GET)
	public Page<BlackUserDto> pageFindBlacks(@RequestParam(name = "page_num", defaultValue = "0") int page,
			@RequestParam(name = "page_size", defaultValue = "20") int size,
			@RequestParam(name = "search_nick", required = false) final String searchNick) {
		Page<BlackList> pos = null;
		String date = "2017-09-09";

		if (StringUtils.isNotBlank(searchNick)) {
			pos = this.blackListRepository.findByNickLikeOrderByCreateTimeDesc(searchNick, new PageRequest(page, size));
		} else {
			pos = this.blackListRepository.findByCreateTimeAfterOrderByCreateTimeDesc(DateUtil.parseDate2date(date),
					new PageRequest(page, size));
		}

		List<PurchaseOrder> orders = new ArrayList<PurchaseOrder>();
		if (pos != null) {
			List<String> nicks = new ArrayList<String>();
			for (BlackList po : pos) {
				nicks.add(po.getNick());
			}
			if (nicks != null && nicks.size() > 0) {
				orders = this.purchaseOrderRepository.findNickByPurchaseTime(nicks);
			}
		}

		Map<String, PurchaseOrder> map = new HashMap<String, PurchaseOrder>();
		if (orders != null && orders.size() > 0) {
			for (PurchaseOrder order : orders) {
				map.put(order.getNick(), order);
			}
		}

		List<BlackUserDto> dtos = new ArrayList<BlackUserDto>();
		if (pos != null) {
			for (BlackList po : pos) {
				PurchaseOrder order = map.get(po.getNick());
				if (order == null) {
					continue;
				}
				BlackUserDto dto = new BlackUserDto();
				dto.from(order);
				dto.from(po);
				dtos.add(dto);
			}
		}
		return new PageImpl<BlackUserDto>(dtos, new PageRequest(page, size), pos.getTotalElements());
	}
}
